@echo off
chcp 65001 >nul
setlocal enabledelayedexpansion

:: 通用AOT发布批处理文件
:: 使用方法: publish-aot.bat [项目名称] [目标运行时] [配置]
:: 示例: publish-aot.bat ConsoleAppDI win-x64 Release

echo ========================================
echo 通用AOT发布工具
echo ========================================

:: 设置默认参数
set "PROJECT_NAME=%1"
set "RUNTIME=%2"
set "CONFIGURATION=%3"

:: 如果没有提供参数，使用默认值
if "%PROJECT_NAME%"=="" (
    echo 请输入项目名称 (例如: ConsoleAppDI):
    set /p PROJECT_NAME=
)

if "%RUNTIME%"=="" (
    echo 请选择目标运行时:
    echo 1. win-x64 (Windows x64)
    echo 2. linux-x64 (Linux x64)
    echo 3. osx-x64 (macOS x64)
    echo 4. linux-arm64 (Linux ARM64)
    echo 5. osx-arm64 (macOS ARM64)
    set /p RUNTIME_CHOICE=
    
    if "!RUNTIME_CHOICE!"=="1" set "RUNTIME=win-x64"
    if "!RUNTIME_CHOICE!"=="2" set "RUNTIME=linux-x64"
    if "!RUNTIME_CHOICE!"=="3" set "RUNTIME=osx-x64"
    if "!RUNTIME_CHOICE!"=="4" set "RUNTIME=linux-arm64"
    if "!RUNTIME_CHOICE!"=="5" set "RUNTIME=osx-arm64"
    if "!RUNTIME_CHOICE!"=="" set "RUNTIME=win-x64"
)

if "%CONFIGURATION%"=="" (
    echo 请选择配置 (Release/Debug):
    set /p CONFIGURATION=
    if "!CONFIGURATION!"=="" set "CONFIGURATION=Release"
)

:: 验证项目是否存在
set "PROJECT_PATH=src\Services\%PROJECT_NAME%\%PROJECT_NAME%.csproj"
if not exist "!PROJECT_PATH!" (
    echo 错误: 项目文件不存在: !PROJECT_PATH!
    echo 请检查项目名称是否正确
    pause
    exit /b 1
)

:: 设置输出目录
set "OUTPUT_DIR=publish\%PROJECT_NAME%-%RUNTIME%-%CONFIGURATION%"

echo.
echo ========================================
echo 发布配置:
echo 项目名称: %PROJECT_NAME%
echo 目标运行时: %RUNTIME%
echo 配置: %CONFIGURATION%
echo 输出目录: %OUTPUT_DIR%
echo ========================================
echo.

:: 确认是否继续
echo 是否继续发布? (Y/N):
set /p CONFIRM=
if /i not "!CONFIRM!"=="Y" (
    echo 发布已取消
    pause
    exit /b 0
)

echo.
echo ========================================
echo 开始发布AOT版本...
echo ========================================

:: 清理之前的发布
if exist "!OUTPUT_DIR!" (
    echo 清理之前的发布文件...
    rmdir /s /q "!OUTPUT_DIR!"
)

:: 创建输出目录
mkdir "!OUTPUT_DIR!" 2>nul

:: 执行发布命令
echo 正在发布 %PROJECT_NAME% 到 %RUNTIME%...
dotnet publish "!PROJECT_PATH!" ^
    --configuration %CONFIGURATION% ^
    --runtime %RUNTIME% ^
    --self-contained true ^
    --output "!OUTPUT_DIR!" ^
    --verbosity normal

if %ERRORLEVEL% neq 0 (
    echo.
    echo ========================================
    echo 发布失败! 错误代码: %ERRORLEVEL%
    echo ========================================
    pause
    exit /b %ERRORLEVEL%
)

echo.
echo ========================================
echo 发布成功!
echo ========================================
echo 输出目录: %OUTPUT_DIR%
echo.

:: 显示文件信息
if exist "!OUTPUT_DIR!\%PROJECT_NAME%.exe" (
    echo 可执行文件: %PROJECT_NAME%.exe
    for %%F in ("!OUTPUT_DIR!\%PROJECT_NAME%.exe") do (
        echo 文件大小: %%~zF 字节
    )
) else if exist "!OUTPUT_DIR!\%PROJECT_NAME%" (
    echo 可执行文件: %PROJECT_NAME%
    for %%F in ("!OUTPUT_DIR!\%PROJECT_NAME%") do (
        echo 文件大小: %%~zF 字节
    )
)

echo.
echo 文件列表:
dir "!OUTPUT_DIR!" /b

echo.
echo ========================================
echo 发布完成!
echo ========================================

:: 询问是否打开输出目录
echo 是否打开输出目录? (Y/N):
set /p OPEN_DIR=
if /i "!OPEN_DIR!"=="Y" (
    explorer "!OUTPUT_DIR!"
)

pause 